{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Remove Digit From Number to Maximize Result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #greedy #string #enumeration"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #贪心 #字符串 #枚举"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: removeDigit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #移除指定数字得到的最大结果"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个表示某个正整数的字符串 <code>number</code> 和一个字符 <code>digit</code> 。</p>\n",
    "\n",
    "<p>从 <code>number</code> 中 <strong>恰好</strong> 移除 <strong>一个</strong> 等于&nbsp;<code>digit</code> 的字符后，找出并返回按 <strong>十进制</strong> 表示 <strong>最大</strong> 的结果字符串。生成的测试用例满足 <code>digit</code> 在 <code>number</code> 中出现至少一次。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"123\", digit = \"3\"\n",
    "<strong>输出：</strong>\"12\"\n",
    "<strong>解释：</strong>\"123\" 中只有一个 '3' ，在移除 '3' 之后，结果为 \"12\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"1231\", digit = \"1\"\n",
    "<strong>输出：</strong>\"231\"\n",
    "<strong>解释：</strong>可以移除第一个 '1' 得到 \"231\" 或者移除第二个 '1' 得到 \"123\" 。\n",
    "由于 231 &gt; 123 ，返回 \"231\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>number = \"551\", digit = \"5\"\n",
    "<strong>输出：</strong>\"51\"\n",
    "<strong>解释：</strong>可以从 \"551\" 中移除第一个或者第二个 '5' 。\n",
    "两种方案的结果都是 \"51\" 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= number.length &lt;= 100</code></li>\n",
    "\t<li><code>number</code> 由数字 <code>'1'</code> 到 <code>'9'</code> 组成</li>\n",
    "\t<li><code>digit</code> 是 <code>'1'</code> 到 <code>'9'</code> 中的一个数字</li>\n",
    "\t<li><code>digit</code> 在 <code>number</code> 中出现至少一次</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [remove-digit-from-number-to-maximize-result](https://leetcode.cn/problems/remove-digit-from-number-to-maximize-result/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [remove-digit-from-number-to-maximize-result](https://leetcode.cn/problems/remove-digit-from-number-to-maximize-result/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"123\"\\n\"3\"', '\"1231\"\\n\"1\"', '\"551\"\\n\"5\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        return max(number[:i]+number[i+1:] for i,j in enumerate(number) if j==digit)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        ans = '0'\n",
    "        for i,x in enumerate(number):\n",
    "            if digit == x:\n",
    "                ans = max(ans , number[:i] + number[i+1:])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = ''\n",
    "        n = len(number)\n",
    "        for i in range(n):\n",
    "            if digit == number[i]:\n",
    "                tmp = number[0:i] + number[i+1:]\n",
    "                if tmp > res:\n",
    "                    res = tmp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = []\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                num = int(number[0:i] + number[i+1::])\n",
    "                res.append(num)\n",
    "        return str(max(res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        nums = []\n",
    "        for i, char in enumerate(number):\n",
    "            if char == digit:\n",
    "                nums.append(int(number[:i] + number[i+1:]))\n",
    "        return str(max(nums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        last = 0\n",
    "        n = len(number) - 1\n",
    "        for i, c in enumerate(number):\n",
    "            if c == digit:\n",
    "                last = i\n",
    "                if i < n and number[i+1] > c:\n",
    "                    break\n",
    "        return number[:last] + number[last+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        res = \"\"   # 可以得到的最大结果\n",
    "        for i in range(n):\n",
    "            if number[i] == digit:\n",
    "                tmp = number[:i] + number[i+1:]\n",
    "                res = max(res, tmp)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        last=-1\n",
    "        for i in range(len(number)):\n",
    "            if number[i]==digit:\n",
    "                last=i\n",
    "                if i<len(number)-1 and number[i]<number[i+1]:\n",
    "                    return number[:i] + number[i+1:]\n",
    "        return number[:last]+number[last+1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        return max(number[:index] + number[index+1:] for index, char in enumerate(number) if char == digit)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = []\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                res.append(number[:i] + number[i+1:])\n",
    "        return max(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = ''\n",
    "        n = len(number)\n",
    "        for i in range(n):\n",
    "            if digit == number[i]:\n",
    "                tmp = number[0:i] + number[i+1:]\n",
    "                if tmp > res:\n",
    "                    res = tmp\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        last = -1   # 最后一个可删除的下标\n",
    "        for i in range(n):\n",
    "            if number[i] == digit:\n",
    "                last = i\n",
    "                if i < n - 1 and number[i] < number[i+1]:\n",
    "                    return number[:i] + number[i+1:]\n",
    "        return number[:last] + number[last+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, s: str, digit: str) -> str:\n",
    "        return max(s[:i] + s[i + 1:] for i, ch in enumerate(s) if ch == digit)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        ans = []\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                n = number[:i]+number[i+1:]\n",
    "                ans.append(int(n))\n",
    "        return str(max(ans))\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        p0 = 0\n",
    "        r = 0\n",
    "        while(True):\n",
    "            res = number.find(digit,p0,len(number))\n",
    "            if res==-1:\n",
    "                break\n",
    "            else:\n",
    "                p0 = res + 1\n",
    "                if int(number[0:res]+number[res+1:])>r:\n",
    "                    r = int(number[0:res]+number[res+1:])\n",
    "        return str(r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\t\tdef removeDigit(self, number: str, digit: str) -> str:\n",
    "\t\t\tn = len(number)\n",
    "\t\t\tlast = -1   # 最后一个可删除的下标\n",
    "\t\t\tfor i in range(n):\n",
    "\t\t\t\tif number[i] == digit:\n",
    "\t\t\t\t\tlast = i\n",
    "\t\t\t\t\tif i < n - 1 and number[i] < number[i+1]:\n",
    "\t\t\t\t\t\treturn number[:i] + number[i+1:]\n",
    "\t\t\treturn number[:last] + number[last+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = 0\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                tmp = int(number[:i] + number[i+1:])\n",
    "                res = max(res,tmp)\n",
    "        return str(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        l=len(number)\n",
    "        for i in range(l-1):\n",
    "            if number[i]==digit:\n",
    "                t=i \n",
    "                if number[i+1]>digit:\n",
    "                    return number[:i]+number[i+1:]\n",
    "        if number[-1]==digit:\n",
    "            return number[:l-1]\n",
    "        return number[:t]+number[t+1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        bigger = []\n",
    "        smaller = []\n",
    "        for i in range(len(number)-1):\n",
    "            if number[i] == digit:\n",
    "                if int(number[i+1]) > int(number[i]):\n",
    "                    bigger.append(i)\n",
    "                elif int(number[i+1]) < int(number[i]):\n",
    "                    smaller.append(i)\n",
    "        if number[-1] == digit:\n",
    "            smaller.append(len(number)-1)\n",
    "        #print(bigger, smaller)\n",
    "        if len(bigger) > 0:\n",
    "            remove = bigger[0]\n",
    "        else:\n",
    "            remove = smaller[-1]\n",
    "        return number[:remove] + number[remove+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, s: str, digit: str) -> str:\n",
    "        res=''\n",
    "        for i in range(len(s)):\n",
    "            if s[i]==digit:\n",
    "                temp=s[:i]+s[i+1:]\n",
    "                res=max(res,temp)\n",
    "        return res\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        import copy\n",
    "        number=list(number)\n",
    "        a = []\n",
    "        for i in range(len(number)):\n",
    "            b = copy.deepcopy(number)\n",
    "            if b[i] == digit:\n",
    "                b.pop(i)\n",
    "                a.append(int(''.join(b)))\n",
    "        a.sort()\n",
    "        return str(a[-1])\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        last = 0\n",
    "        n = len(number)\n",
    "        for i, c in enumerate(number):\n",
    "            if c == digit:\n",
    "                last = i\n",
    "                if i < n - 1 and number[i+1] > c:\n",
    "                    break\n",
    "        return number[:last] + number[last+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        ans = 0\n",
    "        number = list(str(number))\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                new = number[:]\n",
    "                new.pop(i)\n",
    "                a = int(''.join(new))\n",
    "                if a > ans:\n",
    "                    ans = a\n",
    "        return str(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n=len(number)\n",
    "        lst=[]\n",
    "        for i in range(n):\n",
    "            lst.append(number[i])\n",
    "        print(number)\n",
    "        print(lst)\n",
    "        cor=[]\n",
    "        for i in range(n):\n",
    "            if lst[i]==digit:\n",
    "                cor.append(i)\n",
    "        maxs=0\n",
    "        lst2=[]\n",
    "        for i in cor:\n",
    "            lst2+=lst\n",
    "            lst2.pop(i)\n",
    "            #print(lst2)\n",
    "            a=\"\"\n",
    "            #print(lst2)\n",
    "            for i in range(n-1):\n",
    "                a+=str(lst2[i])\n",
    "                #print(lst2[i])\n",
    "            a=int(a)\n",
    "            maxs=max(a,maxs)\n",
    "            lst2.clear()\n",
    "        return str(maxs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        s = ''\n",
    "        for i in range(n):\n",
    "            if number[i] == digit:\n",
    "                remove_index = i\n",
    "                if i + 1 < n and number[i+1] > digit:\n",
    "                    break\n",
    "        return s + number[:remove_index] + number[remove_index+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        result_list = []\n",
    "        for i in range(0,len(number)):\n",
    "            if number[i] == digit:\n",
    "                result = number[0:i]+number[i:].replace(digit,'',1)\n",
    "                result_list.append(int(result))\n",
    "        return str(max(result_list))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        max_num = ''\n",
    "        for i,num in enumerate(number):\n",
    "            if num==digit:\n",
    "                max_num = max(max_num,number[0:i]+number[i+1:])\n",
    "        return max_num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        for i in range(n):\n",
    "            if number[i]==digit:\n",
    "                res = number[:i]+number[i+1:]\n",
    "                if i<n-1 and number[i+1]>number[i]:\n",
    "                    return number[:i]+number[i+1:]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        l=[]\n",
    "        for i , x in enumerate(number):\n",
    "            if x==digit:\n",
    "                l.append(int(number[:i]+number[i+1:]))\n",
    "        return str(max(l))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        last = -1   # 最后一个可删除的下标\n",
    "        for i in range(n):\n",
    "            if number[i] == digit:\n",
    "                last = i\n",
    "                if i < n - 1 and number[i] < number[i+1]:\n",
    "                    return number[:i] + number[i+1:]\n",
    "        return number[:last] + number[last+1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        ans = 0\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                ans = max(ans, int(number[:i] + number[i + 1:]))\n",
    "        return str(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        ans=[]\n",
    "        for i in range(len(number)):\n",
    "            if number[i]==digit:\n",
    "                ans.append(int( number[0:i]+number[i+1:]))\n",
    "        return str(max(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res, ans = 0, ''\n",
    "        n, start = len(number), 0\n",
    "\n",
    "        while start < n:\n",
    "            if start + 1 < n and number[start] == digit: \n",
    "                res = max(int(ans + number[start + 1:]), res)\n",
    "            ans += number[start]\n",
    "\n",
    "            start += 1\n",
    "\n",
    "        if number[-1] == digit:\n",
    "            res = max(res, int(number[:-1]))\n",
    "\n",
    "        if number[0] == digit:\n",
    "            res = max(int(number[1:]), res)\n",
    "\n",
    "        return str(res)\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        last = -1   # 最后一个可删除的下标\n",
    "        for i in range(n):\n",
    "            if number[i] == digit:\n",
    "                last = i\n",
    "                if i < n - 1 and number[i] < number[i+1]:\n",
    "                    return number[:i] + number[i+1:]\n",
    "        return number[:last] + number[last+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = []\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                res.append(number[:i] + number[i+1:])\n",
    "        return sorted(res)[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        arr = []\n",
    "\n",
    "        remove = -1\n",
    "        for i, c in enumerate(number):\n",
    "            if c == digit: \n",
    "                if i != n-1 and number[i] < number[i+1]:\n",
    "                    remove = i\n",
    "                    break\n",
    "                else:\n",
    "                    remove = i\n",
    "\n",
    "        ans = ''\n",
    "\n",
    "        for i in range(n):\n",
    "            if i != remove:\n",
    "                ans += number[i]\n",
    "\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "\n",
    "        indices = [i for i, num in enumerate(number) if num == digit]\n",
    "\n",
    "        max_num = \"\"\n",
    "\n",
    "        for index in indices:\n",
    "            # 生成一个新的数值，该数值是移除了当前索引处的digit后得到的\n",
    "            new_num = number[:index] + number[index+1:]\n",
    "        \n",
    "            # 如果这个新生成的数值比之前的最大值要大，就更新最大值\n",
    "            if new_num > max_num:\n",
    "                max_num = new_num\n",
    "            \n",
    "        # 返回找到的最大数值字符串\n",
    "        return max_num\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = []\n",
    "        ans = ''\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                res.append(i)\n",
    "        for j in res:\n",
    "            if number[:j] + number[j+1:] > ans:\n",
    "                ans = number[:j] + number[j+1:]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        list1=[]\n",
    "        list2=[]\n",
    "        list3=[]\n",
    "        for i in range(len(number)):\n",
    "            if (digit == number[i]):\n",
    "                list1.append(i)\n",
    "            list2.append(number[i])\n",
    "        for j in list1:\n",
    "            l2_copy=deepcopy(list2)\n",
    "            del l2_copy[j]\n",
    "            str1=''.join(l2_copy)\n",
    "            list3.append(str1)\n",
    "        list4=[int(x) for x in list3]\n",
    "\n",
    "        return list3[list3.index(str(max(list4)))]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n=len(number)\n",
    "        for i in range(n-1):\n",
    "            if number[i+1]>number[i] and number[i]==digit:\n",
    "                return number[0:i]+number[i+1:]\n",
    "        for i in range(n-1,-1,-1):\n",
    "            if number[i]==digit:\n",
    "                return number[0:i]+number[i+1:]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        newNums=[]\n",
    "        for i in range(0,len(number)):\n",
    "            if number[i]==digit:\n",
    "                newNums.append(int(number[0:i]+number[i+1:len(number)]))\n",
    "        return str(max(newNums))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        res = \"\"\n",
    "        for i in range(n):\n",
    "            if number[i]==digit:\n",
    "                tmp = number[:i]+number[i+1:]\n",
    "                res = max(res,tmp)\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        return max(number[:i]+number[i+1:] for i,num in enumerate(number) if num==digit)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        max_number = \"\"\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                new_number = number[:i] + number[i+1:]\n",
    "                if new_number > max_number:\n",
    "                    max_number = new_number\n",
    "        return max_number"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        idxs = []\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                idxs.append(i)\n",
    "        if len(idxs) < 2:\n",
    "            return number[0:idxs[0]] + number[idxs[0]+1:len(number)]\n",
    "        else:\n",
    "            result = []\n",
    "            for idx in idxs:\n",
    "                result.append(number[0:idx] + number[idx+1:len(number)])\n",
    "            return max(result)\n",
    "\n",
    "        return number\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        arr = []\n",
    "        for i, ch in enumerate(number):\n",
    "            if ch == digit:\n",
    "                k = number[:i] + number[i+1:]\n",
    "                arr.append(k)\n",
    "        return max(arr)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res, ans = 0, ''\n",
    "        n, start = len(number), 0\n",
    "\n",
    "        while start < n:\n",
    "            if start + 1 < n and number[start] == digit:\n",
    "             \n",
    "                res = max(int(ans + number[start + 1:]), res)\n",
    "                ans += number[start]\n",
    "            else:\n",
    "                ans += number[start]\n",
    "\n",
    "            start += 1\n",
    "\n",
    "        if number[-1] == digit:\n",
    "            res = max(res, int(number[:-1]))\n",
    "\n",
    "        if number[0] == digit:\n",
    "            res = max(int(number[1:]), res)\n",
    "\n",
    "        return str(res)\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        maxval=0\n",
    "        s=list(number)\n",
    "        for i in range(len(s)):\n",
    "            if s[i]==digit:\n",
    "                s[i]=''\n",
    "                maxval=max(maxval,int(''.join(s)))\n",
    "                s[i]=digit\n",
    "        return str(maxval)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# ### my:双指针 105/112--pass\n",
    "# class Solution:\n",
    "#     def removeDigit(self, number: str, digit: str) -> str:\n",
    "#         n=len(number)\n",
    "#         i,j=0,n-1\n",
    "#         while i<n and number[i]!=digit:\n",
    "#             i+=1\n",
    "#         if i<n-1 and number[i]<number[i+1] or i==n-1:\n",
    "#             return number[:i]+number[i+1:] if i<n-1 else number[:i]\n",
    "#         while j>=0 and number[j]!=digit:\n",
    "#             j-=1\n",
    "#         return number[:j]+number[j+1:]\n",
    "\n",
    "\n",
    "### my:双指针 ---pass\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n=len(number)\n",
    "        for i in range(n):\n",
    "            if number[i]!=digit:\n",
    "                continue\n",
    "            if i<n-1 and number[i]<number[i+1] or i==n-1:\n",
    "                return number[:i]+number[i+1:] if i<n-1 else number[:i]\n",
    "        for j in range(n-1,-1,-1):\n",
    "            if number[j]!=digit:\n",
    "                continue \n",
    "            return number[:j]+number[j+1:]\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        return max(number[:i]+number[i+1:] for i,c in enumerate(number) if c==digit)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res=\"\"\n",
    "        for i in range(len(number)):\n",
    "            if number[i]==digit:\n",
    "                c=number[:i]+number[i+1:]\n",
    "                res=max(res,c)\n",
    "        # lst=sorted(ls,reverse=True)\n",
    "        return res\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        max = \"-1\"\n",
    "        for i in range(len(number)):\n",
    "             if number[i] == digit:\n",
    "                num = number[:i] + number[i + 1:]\n",
    "                if num > max:\n",
    "                    max = num\n",
    "        return max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        arr1 = list(number)\n",
    "        arr2 = []\n",
    "        result = []\n",
    "        num = 0\n",
    "        \n",
    "        for i in range(len(arr1)):\n",
    "            if arr1[i] == digit:\n",
    "                arr2.append(i)\n",
    "                num += 1\n",
    "\n",
    "        for i in range(num):\n",
    "            arr3 = list(number)\n",
    "            del arr3[arr2[i]]\n",
    "            result.append(\"\".join(arr3))\n",
    "        return max(result)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        last = 0\n",
    "        for i in range(n):\n",
    "            if i < n - 1 and number[i] == digit and ord(number[i]) < ord(number[i + 1]):\n",
    "                return number[:i] + number[i + 1:]\n",
    "            if number[i] == digit:\n",
    "                last = i\n",
    "        return number[:last] + number[last + 1:]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = 0\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                tmp = int(number[:i] + number[i+1:])\n",
    "                res = max(res,tmp)\n",
    "        return str(res)\n",
    "\n",
    "\n",
    "\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res=[]\n",
    "        for i in range(len(number)):\n",
    "            if number[i]==digit:\n",
    "                res.append(int(number[:i]+number[i+1:]))\n",
    "        return str(max(res))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        c=number.count(digit)\n",
    "        ls=[]\n",
    "        for i in range(len(number)):\n",
    "            if number[i]==digit:\n",
    "                c=number[:i]+number[i+1:]\n",
    "                ls.append(int(c))\n",
    "        lst=sorted(ls,reverse=True)\n",
    "        return str(lst[0])\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        x=0\n",
    "        for i in range(0,len(number)):\n",
    "            if(number[i]==digit[0]):\n",
    "                x=i\n",
    "                if(i+1<len(number) and ord(number[i])<ord(number[i+1])):\n",
    "                    return ''.join([number[:i],number[i+1:]])\n",
    "        return ''.join([number[:x],number[x+1:]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res=0\n",
    "        for i in range(len(number)):\n",
    "            if number[i]==digit:\n",
    "                tmp=int(number[:i]+number[i+1:])\n",
    "                res=max(res,tmp)\n",
    "        return str(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        ans = 0\n",
    "        for i, x in enumerate(number):\n",
    "            if str(x) == digit:\n",
    "                ans = max(ans, int(number[:i] + number[i+1:]))\n",
    "        return str(ans)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        n = len(number)\n",
    "        res = \"\"   # 可以得到的最大结果\n",
    "        for i in range(n):\n",
    "            if number[i] == digit:\n",
    "                tmp = number[:i] + number[i+1:]\n",
    "                res = max(res, tmp)\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        for i in range(len(number)-1):\n",
    "            if number[i] == digit:\n",
    "                if number[i+1] > number[i]:\n",
    "                    return number[:i] + number[i+1:]\n",
    "                x = i\n",
    "        return number[:-1] if number[-1] == digit else number[:x]+number[x+1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, s: str, digit: str) -> str:\n",
    "        mx = 0\n",
    "        for i, c in enumerate(s):\n",
    "            if c == digit:\n",
    "                mx = max(mx, int(s[:i] + s[i+1:]))\n",
    "        return str(mx)\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        \n",
    "        result = '0'\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                temp = number[:i] + number[i+1:]\n",
    "                if int(temp) > int(result):\n",
    "                    result = temp\n",
    "\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        max = \"-1\"\n",
    "        for i in range(len(number)):\n",
    "             if number[i] == digit:\n",
    "                num = number[:i] + number[i + 1:]\n",
    "                if num > max:\n",
    "                    max = num\n",
    "        return max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        abc=[]\n",
    "        n=len(number)\n",
    "        for i in range(0,n):\n",
    "            if number[i]==digit:\n",
    "                s=int(number[:i]+number[i+1:])\n",
    "                abc.append(s)\n",
    "        d=max(abc)\n",
    "        return str(d)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        res = ''\n",
    "        for i in range(len(number)):\n",
    "            if number[i] == digit:\n",
    "                tem = number[:i] + number[i+1:]\n",
    "                res = max(res,tem)\n",
    "        return res\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        idx = []\n",
    "        for i in range(len(number)):\n",
    "             if number[i] == digit:\n",
    "                idx.append(i)\n",
    "        max = \"-1\"\n",
    "        for i in idx:\n",
    "            num = number[:i] + number[i + 1:]\n",
    "            if num > max:\n",
    "                max = num\n",
    "        return max"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        if number.count(digit)==1:\n",
    "            return number[:number.index(digit)] + number[number.index(digit)+1:]\n",
    "        a = 0\n",
    "        for i in range(len(number)-1):\n",
    "            if number[i] == digit and number[i]<number[i+1]:\n",
    "                return number[:i] + number[i+1:]\n",
    "            elif number[i]==digit:\n",
    "                a = i\n",
    "        if number[-1] ==digit:\n",
    "            return number[:-1]\n",
    "        else:\n",
    "            return number[:a] + number[a+1:]\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def removeDigit(self, number: str, digit: str) -> str:\n",
    "        arr1 = list(number)\n",
    "        arr2 = []\n",
    "        result = []\n",
    "        num = 0\n",
    "        \n",
    "        for i in range(len(arr1)):\n",
    "            if arr1[i] == digit:\n",
    "                arr2.append(i)\n",
    "                num += 1\n",
    "\n",
    "        for i in range(num):\n",
    "            arr3 = list(number)\n",
    "            del arr3[arr2[i]]\n",
    "            result.append(\"\".join(arr3))\n",
    "        return max(result)\n",
    "\n"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
